﻿using DBVisitor;
using DBVisitor.Core;
using DBVisitor.DBProvider;
using Mono.Data.Sqlite;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DBVisitorTest
{
    internal class Program
    {
        public const int loots = 10000;
        public static TableService<TestTable> Table;
        public static SqliteConnection Test1DB;
        public static SqliteConnection Test2DB;
        static void Main(string[] args)
        {
            Directory.CreateDirectory(Directory.GetCurrentDirectory() + "\\Vrax\\");
            Test1DB = new SqliteConnection($"uri=file://{Directory.GetCurrentDirectory() + "\\Vrax\\test1.sqlite"},Version=3");
            Test2DB = new SqliteConnection($"uri=file://{Directory.GetCurrentDirectory() + "\\Vrax\\test2.sqlite"},Version=3");

            (Table = DBService.CreateService(new SqliteAccessProvider(Test2DB)).CreateTableService<TestTable>()).EnsureTableStruct();
            DBService.CreateService(new SqliteAccessProvider(Test1DB)).CreateTableService<TestTable>().EnsureTableStruct();

            //do static .cctor
            var t = typeof(DBValue);
            var rowt = typeof(Row<TestTable>);

            new Test2().tryTest();
            new Test1().tryTest();
            Console.ReadLine();
        }
    }
    public class Test1
    {
        public void tryTest()
        {
            int i = Program.loots;
            var sw = new Stopwatch();
            sw.Start();
            while (i-- > 0)
            {
                var db = Program.Test1DB.Clone() as SqliteConnection;
                db.Open();
                using (var com = db.CreateCommand())
                {
                    var t = new TestTable() { ID = i };
                    com.CommandText = $"INSERT INTO TestTable (ID, ID2, ID3, ID4, ID5, ID6, ID7, Name) VALUES ({t.ID}, {t.ID2}, {t.ID3}, {t.ID4}, {t.ID5}, {t.ID6}, {t.ID7}, '{t.Name}')";
                    com.ExecuteNonQuery();
                }
            }
            sw.Stop();
            Console.WriteLine($"Direct execute Insert SQL Commandx{Program.loots} ，use time:{sw.Elapsed.TotalMilliseconds}ms({sw.Elapsed.TotalSeconds}s)");

            sw.Restart();
            i = 1;
            while (i-- > 0)
            {
                var db = Program.Test1DB.Clone() as SqliteConnection;
                db.Open();
                using (var com = db.CreateCommand())
                {
                    com.CommandText = "SELECT * FROM TestTable WHERE ID2=0";
                    using (var re = com.ExecuteReader())
                    {
                        while (re.Read())
                        {
                            _ = new TestTable()
                            {
                                ID = re.GetInt32(re.GetOrdinal("ID")),
                                ID2 = re.GetInt32(re.GetOrdinal("ID2")),
                                ID3 = re.GetInt32(re.GetOrdinal("ID3")),
                                ID4 = re.GetInt32(re.GetOrdinal("ID4")),
                                ID5 = re.GetInt32(re.GetOrdinal("ID5")),
                                ID6 = re.GetInt32(re.GetOrdinal("ID6")),
                                ID7 = re.GetInt32(re.GetOrdinal("ID7")),
                                Name = re.GetString(re.GetOrdinal("Name")),
                            };
                        }
                        //re.GetOrdinal("ID");
                    }
                }
            }
            sw.Stop();
            Console.WriteLine($"Direct execute Query SQL Commandx{1} in {Program.loots} records ，use time:{sw.Elapsed.TotalMilliseconds}ms({sw.Elapsed.TotalSeconds}s)");
        }
    }
    public class Test2
    {
        public void tryTest()
        {
            int i = Program.loots;
            var sw = new Stopwatch();
            sw.Start();
            while (i-- > 0)
            {
                var t = new TestTable() { ID = i };
                Program.Table.Insert(t);
            }
            sw.Stop();
            Console.WriteLine($"DBVisitor execute Insert SQL Commandx{Program.loots} ，use time:{sw.Elapsed.TotalMilliseconds}ms({sw.Elapsed.TotalSeconds}s)");
            sw.Restart();
            i = 1;
            while (i-- > 0)
            {
                foreach (var row in Program.Table.Where("ID2", 0).SelectAllAndAllRecord())
                {
                    _ = row?.Parse();
                }
            }
            sw.Stop();
            Console.WriteLine($"DBVisitor execute Query SQL Commandx{1} in {Program.loots} records ，use time:{sw.Elapsed.TotalMilliseconds}ms({sw.Elapsed.TotalSeconds}s)");
        }
    }

    public class TestTable
    {
        public int ID { get; set; }
        public int ID2 { get; set; }
        public int ID3 { get; set; }
        public int ID4 { get; set; }
        public int ID5 { get; set; }
        public int ID6 { get; set; }
        public int ID7 { get; set; }
        public string Name { get; set; } = "";
    }
}
